package co.edu.eam.proyecto_final_usuario.utilidades;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;

import co.eam.edu.proyecto_final_empresa.web.controladores.BeanComun;

/**
 * Clase Temporal
 * 
 * @author swatit
 * 
 */
@ManagedBean
public class ConsolaBean extends BeanComun implements Serializable {
	/**
	 * ruta del log.
	 */
	private String rutaLog;

	/**
	 * Log
	 */
	private String log;
	
	
	public ConsolaBean() {
		rutaLog="/home/bitnami/stack/jboss/standalone/log/server.log";
	}

	/**
	 * Metodo para cargar el log.
	 */
	public void cargarLog() {
		File f = new File(rutaLog);
		long size = f.length();
		BufferedReader entrada = null;
		try {
			StringBuilder stbld = new StringBuilder();
			entrada = new BufferedReader(new FileReader(f));

			long tamact = 0;
			String linea = null;
			while ((linea = entrada.readLine()) != null) {
				tamact += linea.length();
				if (tamact > 0.2 * size) {
					stbld.append(linea).append("\n");
				}
			}
			log=stbld.toString();

		} catch (Exception e) {
			agregarMensaje("Error cargando el Log:" + e.getMessage(),
					FacesMessage.SEVERITY_ERROR);
		} finally {
			if (entrada != null) {
				try {
					entrada.close();
				} catch (IOException e) {
					agregarMensaje("Error cargando el Log:" + e.getMessage(),
							FacesMessage.SEVERITY_ERROR);
				}
			}
		}
	}

	/**
	 * @return the rutaLog
	 */
	public String getRutaLog() {
		return rutaLog;
	}

	/**
	 * @param rutaLog
	 *            the rutaLog to set
	 */
	public void setRutaLog(String rutaLog) {
		this.rutaLog = rutaLog;
	}

	/**
	 * @return the log
	 */
	public String getLog() {
		return log;
	}

	/**
	 * @param log the log to set
	 */
	public void setLog(String log) {
		this.log = log;
	}

}
